package com.scalar.chiptrack;

import com.scalar.chiptrack.db.DataSource;

import java.io.*;
import java.sql.*;

/**
 Author      : Ch. s.s. sai veerayya
 Date        : 10 feb 2004
 Description : This program will executes all the procedures existing in the storedprocs directory.
 */

public class ProcUpdater {
    public static void main(String[] args) throws Exception {
        Connection conn = null;
        Statement stmt = null;
        BufferedReader br = null;
        int count = 0;
        int tempIndex = 0;
        File files[] = null, dir = null;
        String str1 = "",   str2 = "";
        StringBuffer msg = null, strbf = null;

        try {
            conn = DataSource.getConnection();
            conn.setAutoCommit(true); //Added by Rohan
            msg = new StringBuffer();
            strbf = new StringBuffer();
            stmt = conn.createStatement();
            dir = new File(System.getProperty("procs.directory"));

            if (dir == null || !(dir.exists())) {
                System.out.println("Directory not found");
                System.exit(1);
            } else if (dir.isDirectory()) {
                files = dir.listFiles();
                for (int j = 0; j < files.length; j++) {
                    if (files[j].isFile() && files[j].getName().indexOf('#') < 0) {
                        br = new BufferedReader(new FileReader(files[j]));
                        while ((str2 = br.readLine()) != null) {
                            strbf.append(" " + str2 + "\n");
                        }
                        str1 = strbf.toString();
                        strbf.setLength(0);
                        try {
                            stmt.executeUpdate(str1);
                        } catch (SQLException sqlex) {
                            if (sqlex.getErrorCode() == 2714 || sqlex.getErrorCode() == 208) {
								str1 = str1.replaceAll( "\t", " " ); // This is over come the issue of having tab after create or alter
//str1 = str1.toUpperCase();
                                if (str1.toUpperCase().indexOf("CREATE ") > -1 && sqlex.getErrorCode() == 2714) {
//str1 = str1.replaceFirst("CREATE ","ALTER ");
                                    tempIndex = str1.toUpperCase().indexOf("CREATE ");
                                    str1 = str1.substring(0, tempIndex) + "alter " + str1.substring(tempIndex + 7);
                                } else if (str1.toUpperCase().indexOf("ALTER ") > -1 || sqlex.getErrorCode() == 208) {
//str1 = str1.replaceFirst("ALTER ","CREATE ");
                                    tempIndex = str1.toUpperCase().indexOf("ALTER ");
                                    str1 = str1.substring(0, tempIndex) + "create " + str1.substring(tempIndex + 6);
                                }
                                try {
                                    stmt.executeUpdate(str1);
                                } catch (SQLException sqlex1) {
                                    count++;
                                    msg.append(count + ". " + files[j].getName() + "\n");
                                    msg.append("   @ Error:" + sqlex1.getMessage() + "\n\n");
                                }
                            } else {
                                count++;
                                msg.append(count + ". " + files[j].getName() + "\n");
                                msg.append("    Error:" + sqlex.getMessage() + "\n\n");
                            }
                        }
                        br.close();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
                if (conn != null) {
                    conn.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (count > 0) {
            System.out.println("These Are The Procedures Not Executed\n\n ");
            System.out.println(msg.toString());
        }
    }
}
